﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 画刷
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            SolidColorBrush solidColorBrush = new SolidColorBrush();
            //使用系统自定义的颜色
            solidColorBrush.Color = Colors.Green;
            //或者从Color的FromRgb成员中得到颜色
            solidColorBrush.Color = Color.FromRgb(0, 0x80, 0);
            grid.Background = solidColorBrush;
        }



        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            try
            {
                var gridBrush = grid.Background as LinearGradientBrush;
                if (gridBrush == null) { return; }
                GradientStop gradientStop0 = gridBrush.GradientStops[0];
                GradientStop gradientStop1 = gridBrush.GradientStops[1];
                Point point = e.GetPosition(this);
                double offset = (point.X + point.Y) / (this.Width + this.Height);
                gradientStop0.Offset = offset;
                gradientStop1.Offset = 1 - offset;
            }
            catch (Exception)
            {

            }

        }

        private void ellipse_MouseMove(object sender, MouseEventArgs e)
        {
            Ellipse ellipse = e.Source as Ellipse;
            Point point = e.GetPosition(ellipse);
            double width = ellipse.Width;
            double height = ellipse.Height;
            RadialGradientBrush brush = ellipse.Fill as RadialGradientBrush;
            double x = point.X / width;
            double y = point.Y / height;
            brush.GradientOrigin = new Point(x, y);
        }

        private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            double offset = e.Delta / 3600.0;
            ImageBrush imageBrush = grid.Background as ImageBrush;
            Rect rect = imageBrush.Viewport;
            if (rect.Width + offset <= 0 && rect.Height + offset <= 0)
                return;

            rect.Width += offset;
            rect.Height += offset;
            imageBrush.Viewport = rect;
        }
    }
}